package edu.fim.presentation.view;

import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.AbstractButton;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JScrollPane;

import edu.fim.presentation.controller.ConsumerController;
import edu.fim.presentation.controller.FoodController;
import edu.fim.presentation.model.bean.Consumer;
import edu.fim.presentation.model.bean.Food;

public class MenuConfigForm extends AbstractForm {

	private static final long serialVersionUID = -3492982882501793953L;

	private AbstractForm mainForm;
	private ConsumerController consumerController;
	private FoodController foodController;
	private JList<Food> foods;
	private AbstractButton removeFoodButton;

	public MenuConfigForm(
			String title,
			AbstractForm mainForm,
			ConsumerController consumerController,
			FoodController foodController) {
		super(title);
		this.mainForm = mainForm;
		this.consumerController = consumerController;
		this.foodController = foodController;
		initialize();
		update();
	}

	public void update() {
		updateFoodList();
		updateRemoveButton();
		pack();
	}

	private void updateFoodList() {
		DefaultListModel<Food> foodListModel = new DefaultListModel<Food>();
		for (Food food : consumerController.getCurrentConsumer().getPreferences()) {
			foodListModel.addElement(food);
		}
		foods.setModel(foodListModel);
	}
	
	private void updateRemoveButton() {
		if (foods.isSelectionEmpty()) {
			removeFoodButton.setEnabled(false);
		}
		else {
			removeFoodButton.setEnabled(true);
		}
	}

	protected void initializeDynamicComponents() {
		initializeFoodListComponent();
		initializeRemoveFoodComponent();
	}
	
	private void initializeFoodListComponent() {
		foods = new JList<Food>();
		foods.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				foodListMouseClicked();
			}
		});
	}

	protected void buildLayout() {
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(foods);

		getContentPane().setLayout(
				new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

		getContentPane().add(scrollPane);
		getContentPane().add(createButtonContainer());
	}

	private Container createButtonContainer() {
		Container container = new Container();
		container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));

		container.add(createAddFoodComponent());
		container.add(removeFoodButton);
		container.add(createBackComponent());

		return container;
	}

	private Component createAddFoodComponent() {
		AbstractButton button = new JButton("Add Foods...");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				addFoodComponentActionPerformed();
			}
		});
		return button;
	}

	private void initializeRemoveFoodComponent() {
		removeFoodButton = new JButton("Delete");
		removeFoodButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				removeFoodComponentActionPerformed();
			}
		});
	}

	private Component createBackComponent() {
		AbstractButton button = new JButton("Back");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				backComponentActionPerformed();
			}
		});
		return button;
	}

	private void addFoodComponentActionPerformed() {
		Form form = new AddFoodForm("Food Selection", this, consumerController, foodController);
		form.setVisible(true);
	}

	private void removeFoodComponentActionPerformed() {
		List<Food> foodList = foods.getSelectedValuesList();
		Consumer consumer = consumerController.getCurrentConsumer();
		for (Food food : foodList) {
			consumer.remove(food);
		}
		consumerController.updatePreferences();
		update();
	}
	
	private void foodListMouseClicked() {
		updateRemoveButton();
	}

	private void backComponentActionPerformed() {
		mainForm.update();
		this.setVisible(false);
	}
}